package com.zd.server.sftpserver.controller;

import com.zd.server.sftpserver.config.SftpServerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sftp")
public class SftpController {
    @Autowired
    SftpServerFactory sftpServerFactory;

    /**
     * http://localhost:18888/sftp/start?host=localhost&port=1024&user=root&password=root&rootPath=D%3A%5CAndroid
     * @param host
     * @param port
     * @param user
     * @param password
     * @param rootPath
     * @return
     */
    @GetMapping("/start")
    public String start(String host, int port,String user,String password,String rootPath) {
        sftpServerFactory.addSftpServer(host, port);
        sftpServerFactory.addUser(host,port,user, password, rootPath);
        sftpServerFactory.startSftpServer(host,port);
        return "OK";
    }

    /**
     * http://localhost:18888/sftp/stop?host=localhost&port=1024
     * @param host
     * @param port
     * @return
     */
    @GetMapping("/stop")
    public String stop(String host, int port) {
        sftpServerFactory.stopSftpServer(host, port);
        return "OK";
    }
    @GetMapping("/list")
    public Map list() {
        return sftpServerFactory.mapSftpServer();
    }
}
